package myky.com.doctorapp.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_list.*
import kotlinx.android.synthetic.main.activity_list.view.*
import kotlinx.android.synthetic.main.fragment_remote_list.*
import kotlinx.android.synthetic.main.fragment_remote_list.view.*
import kotlinx.android.synthetic.main.public_common_title.*

import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.RemoteListAdapter
import myky.com.doctorapp.base.BaseFragment
import myky.com.doctorapp.contract.LongRangeListFragmentContract
import myky.com.doctorapp.entity.LongRangeListEntity
import myky.com.doctorapp.entity.PageEntity
import myky.com.doctorapp.presenter.LongRangeListFragmentPresenter
import myky.com.doctorapp.ui.RemoteDetailsActivity
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.RecyclerUtils


/**
 * Description: 远程列表
 * @author wuyang
 * Created by 2019/3/8 10:57
 */


@SuppressLint("ValidFragment")
class LongRangeListFragment(val statusId:String) : BaseFragment<LongRangeListFragmentPresenter, LongRangeListEntity.PayLoadBean>(),LongRangeListFragmentContract.View ,OnRefreshLoadMoreListener,View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {



    lateinit var listAdapter : RemoteListAdapter
    lateinit var remoteList: MutableList<LongRangeListEntity.PayLoadBean.ListBean>


    override fun getPresenter(): LongRangeListFragmentPresenter = LongRangeListFragmentPresenter()
    override fun injectTarget(): Int =R.id.fragment_layout

    override fun doRetry() {

    }

    override fun getLayoutResource(): Int =R.layout.fragment_remote_list
    override fun initView() {
        remoteList=ArrayList()
        listAdapter= RemoteListAdapter(remoteList)
        remote_list_recycler.layoutManager= LinearLayoutManager(activity)
        remote_list_recycler.adapter=listAdapter
        remote_list_refresh.setOnRefreshLoadMoreListener(this)
        remote_list_refresh.setEnableLoadMoreWhenContentNotFull(false)
        listAdapter.onItemChildClickListener=this
    }

    override fun initData(savedInstanceState: Bundle?) {
        showLoadingView()
        mPresenter.premoteList(RecyclerUtils.getSortByFrist(0,10),statusId)

    }

    override fun showDataError(errorMessage: String) {

    }

    override fun showPages(pageEntity: PageEntity) {

    }

    override fun showDataSuccess(datas: LongRangeListEntity.PayLoadBean) {

        if(datas.list!=null) {
            listAdapter.addData(datas.list)
        }else{
            if (remote_list_refresh.state== RefreshState.Loading){
                remote_list_refresh.finishLoadMoreWithNoMoreData()
            }else {
                showEmptyView("空")
            }
        }
        remote_list_refresh.finishLoadMore(10)
        remote_list_refresh.finishRefresh(10)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {

        mPresenter.premoteList(RecyclerUtils.getSortByItem(10),statusId)

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        remoteList.clear()
        mPresenter.premoteList(RecyclerUtils.getSortByFrist(0,9),statusId)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View?, position: Int) {
            var item=listAdapter.data
            var bundle:Bundle= Bundle()
            bundle.putString(Constants.REMOTEID,item[position].remoteId.toString())
            startActivity(RemoteDetailsActivity::class.java,bundle)
    }
    override fun onClick(view: View) {

    }

}
